//
//  AppDelegate.swift
//  BanTangSwiftTest
//
//  Created by 谭启宏 on 16/6/1.
//  Copyright © 2016年 tqh. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,UITabBarControllerDelegate {

    
    var window: UIWindow?

    var tabbarController:UITabBarController!

//MARK:生命周期
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        print(NSHomeDirectory())
        self.window = UIWindow.init(frame: UIScreen.mainScreen().bounds);
        self.createTabBarControl();
        
        self.window?.rootViewController = self.mainViewController()
        self.window?.makeKeyWindow()
        
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(AppDelegate.changeRootViewController), name: "gotohome", object: nil)
        
        return true
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


//MARK:创建标签控制器
    func createTabBarControl(){
        
        self.tabbarController = UITabBarController.init();
        self.tabbarController.delegate = self;
        
        //首页
        let homeVC = HomeViewController.init();
        homeVC.navigationController?.navigationBarHidden = true
        homeVC.tabBarItem = UITabBarItem.init(title: "😄", image: UIImage.init(named: "Home_unselected"), selectedImage: UIImage.init(named: "Home_selected"))
        let homeNav = UINavigationController.init(rootViewController: homeVC)
        
        
        //广场
        let squareVC = SquareViewController.init();
        squareVC.navigationController?.navigationBarHidden = true
        squareVC.tabBarItem = UITabBarItem.init(title: nil, image: UIImage.init(named: "Square_normal"), selectedImage: UIImage.init(named: "Square_selected"))
        let squareNav = UINavigationController.init(rootViewController: squareVC)
        
        //秀
        let showMeVC = UIViewController.init();
        showMeVC.tabBarItem = UITabBarItem.init(title: nil, image: UIImage.init(named: "Show_normal"), selectedImage: UIImage.init(named: "Show_normal"))
        let showMeNav = UINavigationController.init(rootViewController: showMeVC)
        
        
        //消息中心
        
        let messageCenterVC = MessageCenterViewController.init()
        messageCenterVC.tabBarItem = UITabBarItem.init(title: nil, image: UIImage.init(named: "Message_normal"), selectedImage: UIImage.init(named: "Message_normal"))
        let messageCenterNav = UINavigationController.init(rootViewController: messageCenterVC);
        
        //个人中心
        let personCenterVC = PersonCenterViewController.init()
        personCenterVC.tabBarItem = UITabBarItem.init(title: nil, image: UIImage.init(named: "PersonCenter_unlogin"), selectedImage: UIImage.init(named: "PersonCenter_unlogin"))
        let personCenterNav = UINavigationController.init(rootViewController: personCenterVC);
        
        let viewControllerArr = NSArray.init(objects: homeNav,squareNav,showMeNav,messageCenterNav,personCenterNav)
        
        self.tabbarController.tabBar.translucent = false
        
        self.tabbarController.viewControllers = viewControllerArr as! [UINavigationController]
        
        for var i = 0;i<tabbarController.viewControllers?.count ;i++ {
            let nav = tabbarController.viewControllers![i] as! UINavigationController
            nav.navigationBar.translucent = false
            
        }
        
    }
//MARK:UITabBarControllerDelegate
    
    //标签点击的监听
    func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {
        let childArray = tabbarController.viewControllers
        let index = childArray?.indexOf(viewController)
        if index == 2 {
                print("show me")
            self.presentShowMeViewControl(viewController)
            return false
        }else if index == 3 {
            print("load net")
        }
        return true
        
    }
    
    //点击推送出一个showMe新界面
    func presentShowMeViewControl(viewcontroller:UIViewController) {
        let showMeVC = ShowMeViewController.init()
        let nav = UINavigationController.init(rootViewController: showMeVC)
        self.tabbarController.presentViewController(nav, animated: true, completion: nil)
        
    }
    
//MARK:设置根控制器
    func mainViewController() -> UIViewController {
        
        if NSUserDefaults.standardUserDefaults().objectForKey("FirstStart") != nil {
            return self.tabbarController
        }else {
            NSUserDefaults.standardUserDefaults().setObject(false, forKey: "FirstStart")
            NSUserDefaults.standardUserDefaults().synchronize()
            let first = FirstStartViewController.init()
            return first
        }
        
    }
    
    //引导页面点击后进入主页面
    func changeRootViewController() {
        self.window?.rootViewController = self.tabbarController
    }
    
}

